import * as actionType from "./actionType"
import {loginApi} from "../../http/userApi"

export const loginAction = (user) => ({
  type: actionType.USER_LOGIN,
  user,
})
export const logoutAction = () => ({
  type: actionType.USER_LOGOUT,
})

// 登录异步
export const loginAsyncAction = (user) => (dispatch) =>
  loginApi(user)
    .then((res) => {
      if (res.data.errCode === "200") {
        dispatch(
          loginAction({
            ...res.data.data,
          })
        )
        return Promise.resolve("success")
      } else {
        return Promise.reject(res.data)
      }
    })
    .catch((err) => {
      return Promise.reject(err)
    })
